/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <tools/PLRadixTreeNode.h>

#include <stdio.h>

using namespace PLSDK;

PLRadixTreeNode::~PLRadixTreeNode() {
    delete[] m_pKey;
    
    for(size_t i = 0; i < 16; i++) {
        if(m_pChildren[i] == 0) {
            continue;
        }
        for(size_t j = 0; j < 16; j++) {
            if(m_pChildren[i]->p[j] == 0) {
                continue;
            }
            delete m_pChildren[i]->p[j];
        }
        delete m_pChildren[i];
    }
}

PLRadixTreeNode *PLRadixTreeNode::findChild(const uint8_t *cpKey) {
    // Grab the lookahead token.
    uint8_t token = cpKey[0];
    uint8_t i = (token >> 4) & 0xF;
    uint8_t j = token & 0xF;
    if(m_pChildren[i] == 0) {
        return 0;
    }
    return m_pChildren[i]->p[j];
}

void PLRadixTreeNode::addChild(PLRadixTreeNode *pNode) {
    // Grab the lookahead token.
    uint8_t token = pNode->m_pKey[0];
    uint8_t i = (token >> 4) & 0xF;
    uint8_t j = token & 0xF;
    if(m_pChildren[i] == 0) {
        m_pChildren[i] = new NodePtr;
        memset(reinterpret_cast<uint8_t*>(m_pChildren[i]), 0, sizeof(NodePtr));
    }
    m_pChildren[i]->p[j] = pNode;
    m_nChildren++;
}

void PLRadixTreeNode::replaceChild(PLRadixTreeNode *pNodeOld, PLRadixTreeNode *pNodeNew) {
    // Grab the lookahead token.
    uint8_t token = pNodeOld->m_pKey[0];
    uint8_t i = (token >> 4) & 0xF;
    uint8_t j = token & 0xF;
    if(m_pChildren[i] == 0) {
        printf("PLRadixTree::replaceChild: Algorithmic error.");
        return;
    }
    m_pChildren[i]->p[j] = pNodeNew;
}

void PLRadixTreeNode::removeChild(PLRadixTreeNode *pChild) {
    // Grab the lookahead token.
    uint8_t token = pChild->m_pKey[0];
    uint8_t i = (token >> 4) & 0xF;
    uint8_t j = token & 0xF;
    if(m_pChildren[i] == 0) {
        printf("RadixTree::removeChild: Algorithmic error.");
        return;
    }
    m_pChildren[i]->p[j] = 0;
    m_nChildren--;
}

PLRadixTreeNode::MatchType PLRadixTreeNode::matchKey(const uint8_t *cpKey) {
    if(!m_pKey) {
        return OverMatch;
    }
    size_t i = 0;
    while(cpKey[i] && m_pKey[i]) {
        if(cpKey[i] != m_pKey[i]) {
            return (i==0) ? NoMatch : PartialMatch;
        }
        i++;
    }
    // Why did the loop exit?
    if(cpKey[i] == 0 && m_pKey[i] == 0) {
        return ExactMatch;
    } else if(cpKey[i] == 0) {
        return PartialMatch;
    } else {
        return OverMatch;
    }
}

void PLRadixTreeNode::setKey(const uint8_t *cpKey) {
    if(m_pKey) {
        delete[] m_pKey;
    }
    size_t len = 0;
    while (cpKey[len]) {
        len++;
    }
    m_pKey = new uint8_t[len+1];
    size_t i = 0;
    while(cpKey[i]) {
        m_pKey[i] = cpKey[i];
        i++;
    }
    m_pKey[i] = 0;
}

PLRadixTreeNode *PLRadixTreeNode::getFirstChild() {
    for(size_t i = 0; i < 16; i++) {
        if(m_pChildren[i] == 0) {
            continue;
        }
        for(size_t j = 0; j < 16; j++) {
            if(m_pChildren[i]->p[j] == 0) {
                continue;
            }
            return m_pChildren[i]->p[j];
        }
    }
    return 0;
}

void PLRadixTreeNode::prependKey(const uint8_t *cpKey) {
    size_t curKeyLen = 0;
    while(m_pKey[curKeyLen]) {
        curKeyLen++;
    }
    size_t cpKeyLen = 0;
    while(cpKey[cpKeyLen]) {
        cpKeyLen++;
    }
    uint8_t *pKey = new uint8_t[curKeyLen + cpKeyLen + 1];
    memcpy(pKey, cpKey, cpKeyLen);
    memcpy(&pKey[cpKeyLen], m_pKey, curKeyLen + 1); // +1 to copy the '\0' too.
    if(m_pKey) {
        delete[] m_pKey;
    }
    m_pKey = pKey;
}

PLRadixTreeNode *PLRadixTreeNode::doNext() {
    PLRadixTreeNode *pNode = this;
    while((pNode == this) || (pNode && (pNode->value == 0))) {
        PLRadixTreeNode *tmp;
        if (pNode->m_nChildren) {
            pNode = pNode->getFirstChild();
        } else {
            tmp = pNode;
            pNode = 0;
            while(tmp && tmp->m_pParent != 0 /* Root node */) {
                if((pNode=tmp->getNextSibling()) != 0) {
                    break;
                }
                tmp = tmp->m_pParent;
            }
            if(tmp->m_pParent == 0) {
                return 0;
            }
        }
    }
    return pNode;
}

PLRadixTreeNode *PLRadixTreeNode::getNextSibling() {
    if(!m_pParent) {
        return 0;
    }
    plbool b = false;
    for(size_t i = 0; i < 16; i++) {
        if(m_pParent->m_pChildren[i] == 0) {
            continue;
        }
        for(size_t j = 0; j < 16; j++) {
            if(m_pParent->m_pChildren[i]->p[j] == 0) {
                continue;
            }
            if(b) {
                return m_pParent->m_pChildren[i]->p[j];
            }
            if(m_pParent->m_pChildren[i]->p[j] == this) {
                b = true;
            }
        }
    }
    return 0;
}
